using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web
{
	public class ShopGroupBll
	{
		private Conn conn;
		public ShopGroupBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public ShopGroupBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetAllGroups()
		{
			return this.conn.GetDataTable("Select *,(Select count(*) From [HLShop_Shop] Where HLShop_Shop.ShopGroupID = HL_ShopGroup.ID) as ShopNum From [HL_ShopGroup]");
		}
		public DataTable GetGroups()
		{
			return this.conn.GetDataTable("Select ID,GroupName From [HL_ShopGroup]");
		}
		public DataTable GetGroupByID(int ID)
		{
			return this.conn.GetDataTable("Select * From [HL_ShopGroup] Where ID=" + ID);
		}
		public DataTable GetGroupByName(string Name)
		{
			return this.conn.GetDataTable("Select * From [HL_ShopGroup] Where GroupName=" + Name);
		}
		public string GetGroupPowerById(int GID)
		{
			object scalar = this.conn.GetScalar("Select Power From [HL_ShopGroup] Where ID=" + GID, new IDbDataParameter[0]);
			string result;
			if (scalar != DBNull.Value && scalar != null)
			{
				result = (string)scalar;
			}
			else
			{
				result = string.Empty;
			}
			return result;
		}
		public string GetGroupPowerByShopID(int ShopID)
		{
			object scalar = this.conn.GetScalar("Select Power From [HL_ShopGroup] Where ID=(Select ShopGroupID From HLShop_Shop Where ShopID=" + ShopID + ")", new IDbDataParameter[0]);
			string result;
			if (scalar != DBNull.Value && scalar != null)
			{
				result = (string)scalar;
			}
			else
			{
				result = string.Empty;
			}
			return result;
		}
		public bool CheckGroupPower(string Content, string key)
		{
			bool result;
			if (string.IsNullOrEmpty(Content))
			{
				result = false;
			}
			else
			{
				string[] array = Content.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i].Trim() == key)
					{
						result = true;
						return result;
					}
				}
				result = false;
			}
			return result;
		}
		public bool CheckGroupPower(int ShopID, string key)
		{
			string groupPowerByShopID = this.GetGroupPowerByShopID(ShopID);
			bool result;
			if (string.IsNullOrEmpty(groupPowerByShopID))
			{
				result = false;
			}
			else
			{
				string[] array = groupPowerByShopID.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i].Trim() == key)
					{
						result = true;
						return result;
					}
				}
				result = false;
			}
			return result;
		}
		public T GetPowerByKey<T>(string key, int ShopID)
		{
			object scalar = this.conn.GetScalar(string.Concat(new object[]
			{
				"Select [",
				key,
				"] From [HL_ShopGroup] Where ID =(Select ShopGroupID From HLShop_Shop Where ShopID=",
				ShopID,
				")"
			}), new IDbDataParameter[0]);
			T result;
			if (scalar == DBNull.Value)
			{
				result = default(T);
			}
			else
			{
				if (scalar == null)
				{
					result = default(T);
				}
				else
				{
					result = (T)((object)scalar);
				}
			}
			return result;
		}
		public bool DeleteGroup(int ID)
		{
			return this.conn.Execute("Delete From [HL_ShopGroup] Where ID=" + ID) == 1;
		}
		public int CountShopNum(int GrouID)
		{
			return this.conn.ExecuteCount("Select count(*) From [HLShop_Shop] Where ShopGroupID=" + GrouID);
		}
		public bool AddGroup(string GroupName, string GroupIntro, string Power, int MaxClass, int MaxElite, string TemplatePath, int IsUseSecUrl)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@GroupName", DbType.String, GroupName),
				this.conn.CreateParameter("@GroupIntro", DbType.StringFixedLength, GroupIntro),
				this.conn.CreateParameter("@Power", DbType.StringFixedLength, Power),
				this.conn.CreateParameter("@TemplatePath", DbType.StringFixedLength, TemplatePath),
				this.conn.CreateParameter("@MaxClass", DbType.Int32, MaxClass),
				this.conn.CreateParameter("@MaxElite", DbType.Int32, MaxElite),
				this.conn.CreateParameter("@IsUseSecUrl", DbType.Int32, IsUseSecUrl)
			};
			int num = this.conn.Execute("Insert Into [HL_ShopGroup](GroupName, Power, MaxClass, MaxElite, GroupIntro, TemplatePath,IsUseSecUrl) \r\nvalues(@GroupName, @Power, @MaxClass, @MaxElite, @GroupIntro, @TemplatePath,@IsUseSecUrl)", parameters);
			return num == 1;
		}
		public bool ModifyGroup(int ID, string GroupName, string GroupIntro, string Power, int MaxClass, int MaxElite, string TemplatePath, int IsUseSecUrl)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@GroupName", DbType.String, GroupName),
				this.conn.CreateParameter("@GroupIntro", DbType.String, GroupIntro),
				this.conn.CreateParameter("@Power", DbType.String, Power),
				this.conn.CreateParameter("@TemplatePath", DbType.String, TemplatePath),
				this.conn.CreateParameter("@MaxClass", DbType.Int32, MaxClass),
				this.conn.CreateParameter("@MaxElite", DbType.Int32, MaxElite),
				this.conn.CreateParameter("@IsUseSecUrl", DbType.Int32, IsUseSecUrl)
			};
			int num = this.conn.Execute("Update [HL_ShopGroup] Set GroupName = @GroupName, Power = @Power, MaxClass=@MaxClass, \r\nMaxElite=@MaxElite, GroupIntro = @GroupIntro, TemplatePath = @TemplatePath, IsUseSecUrl = @IsUseSecUrl Where ID=" + ID, parameters);
			return num == 1;
		}
		public DataTable GetGroupShopTempTeleByGroupID(int GroupID)
		{
			return this.conn.GetDataTable("Select * From HL_ShopTemplate Where ShopTempID in ( Select TempID From HL_GroupShopTemplate \r\nWhere GroupShopID = " + GroupID + ")");
		}
		public bool DeleteGroupTempTeleByIDS(string TempTeleIDS)
		{
			return this.conn.Execute("Delete From HL_GroupShopTemplate Where GroupTempTeleID in (" + TempTeleIDS + ")") > 0;
		}
		public bool AddGroupTempTele(int TempID, int GroupShopID)
		{
			string text = string.Format("Select * From HL_GroupShopTemplate Where TempID = {0} and GroupShopID= {1}", TempID, GroupShopID);
			return this.conn.Execute(string.Format("Insert Into HL_GroupShopTemplate (TempID,GroupShopID) Values ({0},{1})", TempID, GroupShopID)) > 0;
		}
	}
}
